package com.lsf.dunlian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lsf.dunlian.R;
import com.lsf.dunlian.adapter.MemberDetailProgressRecyclerViewAdapter;
import com.lsf.dunlian.adapter.MemberListRecyclerViewAdapter;
import com.lsf.dunlian.adapter.MemberListTitleRecyclerViewAdapter;
import com.lsf.dunlian.config.Urls;
import com.lsf.dunlian.databinding.ActivityMemberDetailBinding;
import com.lsf.dunlian.entity.BasicMember;
import com.lsf.dunlian.entity.HouseModel;
import com.lsf.dunlian.entity.MemberDetailProgressEntity;
import com.lsf.dunlian.entity.MemberListTitleEntity;
import com.mylhyl.superdialog.SuperDialog;
import com.mylhyl.superdialog.res.values.ColorRes;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MemberDetailActivity extends FragmentActivity {

    private ActivityMemberDetailBinding binding;
    private MemberDetailProgressRecyclerViewAdapter memberDetailProgressRecyclerViewAdapter;
    private List<MemberDetailProgressEntity> list = new ArrayList<>();
    private List<String> c = Arrays.asList("修改");
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseUtils.setStatusBar(this);
        binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_member_detail, null, false);
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> finish());
        String data = getIntent().getStringExtra("data");
        BasicMember member = new Gson().fromJson(data, BasicMember.class);


        binding.fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SuperDialog.Builder(MemberDetailActivity.this)
                        .setGravity(Gravity.CENTER)
                        .setTitle("操作", ColorRes.negativeButton)
                        .setCanceledOnTouchOutside(false)
                        .setItems(c, position -> {
                            if ("修改".equals(c.get(position))){
                                Intent intent = new Intent(MemberDetailActivity.this, MemberActivity.class);
                                intent.putExtra("data",data);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton("取消", null)
                        .build();
            }
        });

        getDemand(member.getYezhuloucheng());
        memberDetailProgressRecyclerViewAdapter = new MemberDetailProgressRecyclerViewAdapter(list, this);
        binding.progressRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        binding.progressRecyclerView.setAdapter(memberDetailProgressRecyclerViewAdapter);

        binding.address.setText(member.getAddress());
        binding.project.setText(member.getProject());
        binding.huxing.setText(member.getHouseType());
        binding.mianji.setText(member.getMemo());
        binding.lianxiren.setText(member.getLianxiren());
        binding.lianxirenDianhua.setText(member.getLianxirenDianhua());
        binding.xingbie.setText(member.getJinzengGongsi());
        binding.kehuzhuangtai.setText(member.getZhuangxiuJindu());
        binding.kehujieduan.setText(member.getYezhuloucheng());
        binding.kehuleixing.setText(member.getYezhu());
        binding.kehuxingzhi.setText(member.getYezhuDianhua());
        binding.kehulaiyuan.setText(member.getJiechuRenqun());
        binding.kehuhangye.setText(member.getJiechuRenqunDianhua());
        binding.shangjikehu.setText(member.getYigouPinpai());
        binding.xingjiLayout.removeAllViews();
        for (int i = 0; i < Integer.parseInt(member.getZhuangxiuGongsi()); i++) {
            binding.xingjiLayout.addView(LayoutInflater.from(MemberDetailActivity.this).inflate(R.layout.item_xing, null, false));
        }
    }
    private void getDemand(String val) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(Urls.MEMBER_GETDEMAND).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    HouseModel vo = new Gson().fromJson(result, HouseModel.class);
                    if (vo.code != 200 || vo.data == null && vo.data.size() == 0) return;
                    int contains = vo.data.indexOf(val);
                    for (int i = 0; i <vo.data.size() ; i++) {
                        if (contains>=i){
                            list.add(new MemberDetailProgressEntity(vo.data.get(i),true));
                        }else {
                            list.add(new MemberDetailProgressEntity(vo.data.get(i),false));
                        }
                    }
                    runOnUiThread(() -> memberDetailProgressRecyclerViewAdapter.notifyDataSetChanged());
                }
            }
        });
    }

}
